import numpy as np
import os,sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from action_set_encoder import agent_json2local_attrs


class Agent(object):
    def __init__(self, team, team_id, uid, location,rotation, velocity, health, yaw, speed) -> None:
        self.location = None
        self.team = team
        self.team_id = team_id
        self.uid = uid
        self.attrs = agent_json2local_attrs
        for attr_json, attr_agent in self.attrs: setattr(self, attr_agent, None)
        self.location = location
        self.velocity = velocity
        self.rotation = rotation
        self.max_speed = speed
        self.pos3d = np.array([location[0], location[1], location[2]])
        self.pos2d = np.array([location[0], location[1]])
        self.vel3d = np.array([velocity[0],velocity[1],velocity[2]])
        self.hp = health
        self.yaw = yaw
        self.speed = speed

        # 2025/07
        # self.pos3d = np.array([np.nan, np.nan, np.nan])
        # self.pos2d = np.array([np.nan, np.nan])
    
    def update_agent_attrs(self, dictionary):
        if (not dictionary['agentAlive']):
            self.alive = False
            # inf = float('inf')
            # self.location = (inf, inf, inf)
        else:
            assert dictionary['valid']
            for attr_json, attr_agent in self.attrs: 
                setattr(self, attr_agent, dictionary[attr_json])
            # print(self.uid)
            # print(self.uid_remote)
            assert self.uid == self.uid_remote
            self.pos3d = np.array(self.location)
            self.pos2d = self.pos3d[:2]
            self.vel3d = np.array(self.velocity)
            self.vel2d = self.pos3d[:2]
            self.scale3d = np.array(self.scale3)
            self.scale = self.scale3[0]
            self.yaw = self.rotation[0]
            self.alive = True
            self.team = dictionary['agentTeam']


